// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1

Rectangle {
    id: page //unique id of the main object
    width: 640 //width of the window
    height: 640 //height of the window
    color: "black" //background color of the window
    property int filterOn: 0 //is filter turned on
    property int spectrogramOn: 0 //is spectrogram turned on

    signal turnFilterOn(int fbandLow, int fbandHigh, int order); //signal for turning filter on
    signal turnFilterOff(); //signal for turning filter off

    signal turnSpectrogramOn(int samples, int length, int delta); //signal for turning spectrogram on
    signal turnSpectrogramOff(); //signal for turning spectrogram off

    property int baselineDuration: 8 * 2 * 30 // duration of the basline recording, 10 seconds (8 samples/sec * 2 channels/sample * 10 seconds)
    property int baselineCollected: 0 //how much data was collected for the baseline
    property double baselinePower: 0 //what is the baseline power
    property int baselineRecording: 0 //is baseline recording on
    property int trialInProgress: 0 //is trial (baseline or test) currently in progress
    property int testDuration: 8 * 2 * 30 //duration of the test, 10 seconds (8 samples/sec * 2 channels/sample * 10 seconds)
    property int testCollected: 0 //how much data was collected for the test
    property double testPower: 0 //what is the test power
    property int finished: 0 //has the trial finished

    property int samplesSeen: 0 //samples counter modulo 80, used only for showing blinking square

    /**
      Function reciving channel name and list of power values.
      It contains the whole logic of power calculation and storing.
      */
    function spectrogramValue(channel, valuelist)
    {
	//counting samples mod 80, used just for visualization of the blinkgin square
	samplesSeen = (samplesSeen + 1)%80;


	//here we sum all the values from the list of power values, i.e. calculate total power in given frequency band
	var sumValues = 0.0;
	for (var v in valuelist)
	    sumValues += parseFloat(valuelist[v]);


	//we are recording baseline
	if (baselineRecording)
	{
	    //update mean power
	    baselinePower = (baselinePower*baselineCollected + sumValues)/(baselineCollected+1);
	    baselineCollected += 1;
	    //we have collected enough samples
	    if (baselineCollected == baselineDuration)
	    {
		baselineRecording = 0;

	    }
	}
	//we are not recording baseline, but the trial is in progress, so we are in test
	else if (trialInProgress)
	{

	    //calculate test power; here we use simple power/baseline - 1 to get value from -1 to +inf
	    testPower += sumValues / baselinePower - 1;
	    testCollected += 1;

	    // we have collected enought samples
	    if (testCollected == testDuration)
	    {
		finished = 1;
		trialInProgress = 0;
	    }
	}

    }

    //grid with buttons
    Grid
    {
	id: controlsGrid
	Rectangle
	{
	    //filtering button
	    color: {if (filterOn) return "red"; return "blue";}
	    width: 100
	    height: 33
	    Text {
		anchors.centerIn: parent
		text: {if (filterOn) return "filter on"; return "filter off";}
		color: "white"
	    }
	    MouseArea
	    {
		anchors.fill: parent
		onClicked:
		{
		    //if filter is currently on, turn it off
		    if (page.filterOn)
		    {
			page.filterOn = 0;
			page.turnFilterOff();
		    }
		    //filter is off, turn it on
		    else
		    {
			page.filterOn = 1;
			//8-13 Hz, 32 samples window
			page.turnFilterOn(8,13,32);
		    }
		}
	    }
	}
	Rectangle
	{
	    //spectrogram button
	    color: {if (spectrogramOn) return "red"; return "blue";}
	    width: 100
	    height: 33
	    Text {
		anchors.centerIn: parent
		text: {if (spectrogramOn) return "spec on"; return "spec off";}
		color: "white"
	    }
	    MouseArea
	    {
		anchors.fill: parent
		onClicked:
		{
		    if (page.spectrogramOn)
		    {
			page.spectrogramOn = 0;
			page.turnSpectrogramOff();
		    }
		    else
		    {
			page.spectrogramOn = 1;
			//turn spectrogram on, 128Hz sampling frequency, window size of 128 samples, performed every 16 samples (8 times/sec)
			page.turnSpectrogramOn(128,128,16);
		    }
		}
	    }
	}

	Rectangle //indicator showing that the spectrogram values are flowing in by chagning color
	{
	    width: 33
	    height: width
	    color: Qt.rgba(samplesSeen/80.0,0,0,1);
	}
    }


    Rectangle
    {
	//button for starting trial and displaying status
	anchors.left: parent.left
	anchors.top: controlsGrid.bottom
	width: 200
	height: 66
	color: {if (page.trialInProgress) return "black"; return "white"}

	//display text, either start or current values
	Text
	{
	    anchors.centerIn: parent
	    color: "red"
	    text: {if (!page.trialInProgress) return "start"; if (page.baselineRecording) return "baseline: "+parseInt(page.baselinePower*10)/10; return "test power: "+parseInt(page.testPower*10)/10;}
	}

	MouseArea
	{
	    anchors.fill: parent
	    onClicked:
	    {
		//stop and reset everything
		if (page.trialInProgress)
		{
		    page.trialInProgress = 0;
		    page.baselineRecording = 0;
		    page.baselineCollected = 0;
		    page.baselinePower = 0;
		    page.testPower = 0;
		    page.testCollected = 0;
		}
		//start trial
		else
		{
		    page.trialInProgress = 0;
		    page.baselineRecording = 0;
		    page.baselineCollected = 0;
		    page.baselinePower = 0;
		    page.testPower = 0;
		    page.testCollected = 0;
		    page.trialInProgress = 1;
		    page.baselineRecording = 1;
		}
	    }
	}

    }

    Rectangle
    {
	//quit button
	color: "red"
	anchors.right: parent.right
	anchors.top: parent.top
	width: 50
	height: 50
	MouseArea
	{
	    anchors.fill: parent
	    onClicked:
	    {
		Qt.quit();
	    }
	}
    }

    Text //helo world text appearing in the middle of the screen when test power gets greater than baseline power
    {
	anchors.centerIn: parent
	font.pixelSize: 60
	color: Qt.rgba(1,1,1,page.testPower/testDuration*10);
	text: "hello world"
	visible: page.trialInProgress || page.finished
    }

}
